package Tree;

import java.util.Arrays;

/**
 * 作者:hfj
 * 功能:108.将有序数组转换为二叉搜索树
 * 日期: 2025/10/24 10:50
 */
public class SortedArrayToBST {

    public TreeNode sortedArrayToBST(int[] nums) {
        int len = nums.length;
        if (len == 0){//长度为0则为空树
            return null;
        }
        int mid = len / 2;
        TreeNode root = new TreeNode(nums[mid]);//取中间值为根节点
        root.left = sortedArrayToBST(Arrays.copyOfRange(nums, 0, mid ));//复制左边的数组到左子树
        root.right = sortedArrayToBST(Arrays.copyOfRange(nums, mid+1, len));//复制右边的数组到右子树
        return root;
    }
}
